package github.leavesczy.reactivehttpsamples.core.viewmodel.main

import androidx.lifecycle.MutableLiveData
import github.leavesczy.reactivehttpsamples.base.BaseViewModel
import github.leavesczy.reactivehttpsamples.core.mode.BridgeRecordData
import github.leavesczy.reactivehttpsamples.core.mode.BridgeRecordItem
import github.leavesczy.reactivehttpsamples.util.Logger
import kotlinx.coroutines.delay

class HistoryViewModel:BaseViewModel() {

    val bridgeRecordData = MutableLiveData<BridgeRecordData?>()
    val bridgeAnalysisData = MutableLiveData<BridgeRecordItem>()
    val bridgeDeleteData = MutableLiveData<Any>()
    val bridgeResetResultData = MutableLiveData<Any>()

    fun requestBridgeRecordList(pageNumber:Int,pageSize: Int){
        remoteDataSource.enqueue({
            Logger.d("协成的线程，api请求："+Thread.currentThread())
            val map= mutableMapOf<String,Any>()
            map.put("pageNum",pageNumber)
            map.put("pageSize",pageSize)
            requireBridgeListPage(createJsonBody(map))
        }){
            onSuccess {
                Logger.d("协成的线程，api回调："+Thread.currentThread())
                bridgeRecordData.value=it
            }
            onFailed {
                bridgeRecordData.value=null
            }
        }
    }

    fun requestBridgeAnalysis(recordId:Long){
        remoteDataSource.enqueueLoadingCancelable({
            val map= mutableMapOf<String,Any>()
            map.put("recordId",recordId)
            requireBridgeAnalysis(createJsonBody(map))
        },showLoadingCancelable = false,{
            onSuccess {
                bridgeAnalysisData.value=it
            }
        })
    }

    fun requireBridgeDelete(recordId: Long) {
        remoteDataSource.enqueueLoadingCancelable({
            val map= mutableMapOf<String,Any>()
            map.put("recordId",recordId)
            requireBridgeDelete(createJsonBody(map))
        }, showLoadingCancelable = false,{
            onSuccess {
                bridgeDeleteData.value=it
            }
        })
    }

    fun requireBridgeResetResult(recordId: Long) {
        remoteDataSource.enqueueLoadingCancelable({
            val map= mutableMapOf<String,Any>()
            map.put("recordId",recordId)
            requireBridgeResetResult(createJsonBody(map))
        }, showLoadingCancelable = false,{
            onSuccess {
                bridgeResetResultData.value=it
            }
        })
    }
}